﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using DevExpress.Web.Mvc;

namespace KimPhongCRM.Controllers
{
    [PermissionFilter]
    public class MainMenuController : BaseController
    {
        private Entities db = new Entities();

        //
        // GET: /MainMenu/
        [MvcSiteMapProvider.MvcSiteMapNode(Title = "Quản lý menu", Key = "Menu", Clickable = true, ParentKey = "Home")]
        public ViewResult Index()
        {
            return View();
        }

        //
        // GET: /MainMenu/Display

        public ViewResult Display()
        {
            List<sp_Select_MainMenu_Parent_Result> lstMenuParent = db.sp_Select_MainMenu_Parent().ToList();
            Dictionary<long, List<sp_Select_MainMenu_Children_Result>> dicMenuChild = new Dictionary<long, List<sp_Select_MainMenu_Children_Result>>();
            foreach (var item in lstMenuParent)
            {
                dicMenuChild.Add(item.MenuId, db.sp_Select_MainMenu_Children(item.MenuId).ToList());
            }
            Tuple<List<sp_Select_MainMenu_Parent_Result>, Dictionary<long, List<sp_Select_MainMenu_Children_Result>>> tuple
                = new Tuple<List<sp_Select_MainMenu_Parent_Result>, Dictionary<long, List<sp_Select_MainMenu_Children_Result>>>(lstMenuParent, dicMenuChild);
            return View(tuple);
        }

        //
        // GET: /MainMenu/Details/5

        public ViewResult Details(Guid id)
        {
            tblMainMenu tblmainmenu = db.tblMainMenus.Find(id);
            return View(tblmainmenu);
        }

        //
        // GET: /MainMenu/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /MainMenu/Create

        [HttpPost]
        public ActionResult Create(tblMainMenu tblmainmenu)
        {
            if (ModelState.IsValid)
            {
               // tblmainmenu.MenuId = Guid.NewGuid();
                db.tblMainMenus.Add(tblmainmenu);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(tblmainmenu);
        }

        //
        // GET: /MainMenu/Edit/5

        public ActionResult Edit(int id)
        {
            tblMainMenu tblmainmenu = db.tblMainMenus.Find(id);
            return View(tblmainmenu);
        }

        //
        // POST: /MainMenu/Edit/5

        [HttpPost]
        public ActionResult Edit(tblMainMenu tblmainmenu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblmainmenu).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tblmainmenu);
        }

        //
        // GET: /MainMenu/Delete/5

        public ActionResult Delete(int id)
        {
            tblMainMenu tblmainmenu = db.tblMainMenus.Find(id);
            return View(tblmainmenu);
        }

        //
        // POST: /MainMenu/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            tblMainMenu tblmainmenu = db.tblMainMenus.Find(id);
            db.tblMainMenus.Remove(tblmainmenu);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        [ValidateInput(false)]
        public ActionResult TreeListMainMenuPartial()
        {
            var model = db.tblMainMenus;
            return PartialView("_TreeListMainMenuPartial", model.ToList());
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult TreeListMainMenuPartialAddNew(tblMainMenu item)
        {
            var model = db.tblMainMenus;
            if (ModelState.IsValid)
            {
                try
                {
                    //item.menuId = 0;
                    model.Add(item);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Vui lòng kiểm tra lại dữ liệu.";
            return PartialView("_TreeListMainMenuPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult TreeListMainMenuPartialUpdate(KimPhongCRM.Models.tblMainMenu item)
        {
            var model = db.tblMainMenus;
            if (ModelState.IsValid)
            {
                try
                {
                    var modelItem = model.FirstOrDefault(it => it.menuId == item.menuId);
                    if (modelItem != null)
                    {
                        this.UpdateModel(modelItem);
                        db.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_TreeListMainMenuPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult TreeListMainMenuPartialDelete(long MenuId)
        {
            var model = db.tblMainMenus;
            if (MenuId != null)
            {
                try
                {
                    var item = model.FirstOrDefault(it => it.menuId == MenuId);
                    if (item != null)
                        model.Remove(item);
                    db.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_TreeListMainMenuPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult TreeListMainMenuPartialMove(long MenuId, long ParentId)
        {
            var model = db.tblMainMenus;
            try
            {
                var item = model.FirstOrDefault(it => it.menuId == MenuId);
                if (item != null)
                    item.parentId = ParentId;
                db.SaveChanges();
            }
            catch (Exception e)
            {
                ViewData["EditError"] = e.Message;
            }
            return PartialView("_TreeListMainMenuPartial", model.ToList());
        }
    }
}